from datetime import datetime

from flask import render_template, url_for, flash, redirect
from flask_login import login_required, login_user, logout_user, current_user

from ..modle import TUser, TUserInformation
from . import loginprint as loginview
from .forms import loginForm, registerForm
from .. import db


@loginview.route('/login', methods=["POST", "GET"])
def login():
    loginform = loginForm()
    if loginform.validate_on_submit():
        account = loginform.account.data
        password = loginform.password.data

        from ..modle import TUser
        user = TUser.query.filter_by(user_name=account).first()
        if user is not None and user.verify_password(password):
            login_user(user, loginform.remenber_me)
            return redirect(url_for('main.index'))
        flash('num or password is not correct!')
    return render_template("login.html", form=loginform)


@loginview.route('/logout')
@login_required
def logout():
    if current_user.is_authenticated:
        logout_user()
    return redirect(url_for('main.index'))


@loginview.route('/register', methods=["POST", "GET"])
def register():
    registform = registerForm()
    if registform.validate_on_submit():
        user_id = registform.user_id.data
        user = TUser.query.filter_by(user_id=user_id).first()
        if user is not None:
            flash('this id is exist!')
            return render_template('register.html', form=registform)

        user_name = registform.user_name.data
        user = TUser.query.filter_by(user_name=user_name).first()
        if user is not None:
            flash('this name has been used!')
            return render_template('register.html', form=registform)

        user_email = registform.user_email.data
        user_phonenumber = registform.user_phonenumber.data

        user_info = TUserInformation.query.filter_by(user_email=user_email).first()
        if user_info is not None:
            flash('this email has been used!')
            return render_template('register.html', form=registform)

        user_info = TUserInformation.query.filter_by(user_phonenumber=user_phonenumber).first()
        if user_info is not None:
            flash('this phonenumber has been used!')
            return render_template('register.html', form=registform)

        user_password = registform.user_password1.data
        user_turename = registform.user_truename.data
        user_sex = registform.user_sex.data
        user_birthday = registform.user_birthday.data
        user_profession = registform.user_profession.data
        user_units = registform.user_units.data
        user_location = registform.user_location.data

        user = TUser(user_id=user_id, user_name=user_name)
        user.password(user_password)
        user_info = TUserInformation(
            user_id=user_id, user_truename=user_turename, user_sex=user_sex, user_email=user_email,
            user_phonenumber=user_phonenumber, user_birthday=user_birthday, user_signuptime=datetime.now(),
            user_profession=user_profession, user_units=user_units, user_location=user_location)
        db.session.add(user)
        db.session.commit()
        db.session.add(user_info)
        db.session.commit()
        return redirect(url_for('loginview.login'))

    return render_template('register.html', form=registform)
